<?
    include "../../common.php" ;
    $opt = $_POST['opt'] ; 
    $arr = [] ; 
    if( $opt == 'getList' ){
        $page = $_POST['page'] ; 
        $perPage = $_POST['perPage'] ;
        $pageSt = ( $page - 1 ) * $perPage ;  
        $sql = "SELECT * FROM `users` WHERE is_delete = 1 AND ulevel = 6 ORDER BY id ASC LIMIT {$pageSt},{$perPage}" ;
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ; 
        }
    }
    if( $opt == 'updSel' ){ 
        $id = $_POST['id'] ;
        $sql = "SELECT * FROM `users` WHERE id = {$id}" ;
        $res = mysqli_query($link,$sql) ;
        if( mysqli_affected_rows($link) ){
            $one = mysqli_fetch_assoc($res) ;
            $arr = $one ; 
        }
    }
    if( $opt == 'upd' ){
        $tel = $_POST['tel'];
        $pwd = $_POST['pwd'];
        $ulevel = $_POST['ulevel'];
        $id = $_POST['id'];
        $sql = "UPDATE `users` SET tel = {$tel} , pwd = '{$pwd}' , ulevel = {$ulevel} WHERE id = {$id}" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '修改成功!' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败!' ; 
        }
    }
    if( $opt == 'del' ){
        $id = $_POST['id'] ;
        $sql = "DELETE FROM `users` WHERE id = {$id}" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '删除成功!' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作成功!' ;
        }
    }
    if( $opt == 'getPageNum' ){
        $perPage = $_POST['perPage'] ; 
        $sql = "SELECT COUNT(*) AS c FROM `users` WHERE is_delete = 1 AND ulevel = 6 " ;
        $res = mysqli_query($link,$sql) ;
        if( mysqli_affected_rows($link) ){
            $one = mysqli_fetch_assoc($res) ; 
            $arr['totalPage'] = ceil($one['c'] / $perPage) ; 
        }
    }
    if( $opt == 'getCateList' ){
        $page = $_POST['page'] ; 
        $perPage = $_POST['perPage'] ;
        $pageSt = ( $page - 1 ) * $perPage ;  
        $sql = "SELECT * FROM pcate WHERE subid is null ORDER BY id ASC LIMIT {$pageSt},{$perPage}";
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ; 
        }
    }
    if( $opt == 'addCate' ){
        $cateName = $_POST['cateName'] ;
        $sql = "INSERT INTO pcate SET cateName = '{$cateName}' " ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '添加成功!' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败!' ;
        }
    }
    if( $opt == 'updCate' ){
        $cateName = $_POST['cateName'] ;
        $myid = $_POST['myid'] ;  
        $sql = "UPDATE `pcate` SET `cateName`='{$cateName}' WHERE id = {$myid}" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '修改成功!' ; 
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败!' ; 
        }
    }
    if( $opt == 'delCate' ){
        $id = $_POST['myid'] ;
        $sql = "DELETE FROM pcate WHERE id = {$id}" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $sql1 = "SELECT * FROM pcate WHERE subid = {$id}" ;
            $res1 = mysqli_query($link,$sql1);
            if( mysqli_affected_rows($link) ){
                while( $one = mysqli_fetch_assoc($res1) ){
                    $dataArr[] = $one ; 
                    $arr['data'] = $one ; 
                }
                foreach( $dataArr as $key=>$value ){
                    $sql2 = "DELETE FROM pcate WHERE subid = {$value['id']}" ;
                    $res2 = mysqli_query( $link , $sql2 );
                }
                $sql3 = "DELETE FROM pcate WHERE subid = {$id}" ;
                $res3 = mysqli_query( $link , $sql3 );
            }
            $arr['status'] = 1 ;
            $arr['info'] = '删除成功!' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败!' ;
        }
    }
    if( $opt == 'getCatePage' ){
        $perPage = $_POST['perPage'] ; 
        $sql = "SELECT COUNT(*) AS c FROM pcate WHERE subid IS NULL" ;
        $res = mysqli_query($link,$sql) ;
        if( $one = mysqli_fetch_assoc($res) ){
            $pageNum = ceil( $one['c'] / $perPage ) ; 
            $arr['totalPage'] = $pageNum ; 
        }
    }
    if( $opt == 'getSubList' ){
        $page = $_POST['page'] ; 
        $perPage = $_POST['perPage'] ;
        $pageSt = ( $page - 1 ) * $perPage ;  
        $sql = "SELECT * FROM pcate WHERE subid is not null ORDER BY id ASC LIMIT {$pageSt},{$perPage}";
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ; 
        }
    }
    if( $opt == 'addSub' ){
        $cateId = $_POST['cateId'] ;
        $cateName = $_POST['cateName'] ;
        $subName = $_POST['subName'] ;
        $sql = "INSERT INTO `pcate`(`cateName`, `subid`, `subCateName`) VALUES ('{$cateName}',{$cateId},'{$subName}')" ;
        $res = mysqli_query($link,$sql);
        $arr['sql'] =  $sql ; 
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '添加成功!' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败!' ; 
        }
    }
    if( $opt == 'updSub' ){
        $subName = $_POST['subName'] ;
        $cateId = $_POST['cateId'] ;  
        $cateName = $_POST['cateName'] ; 
        $myid = $_POST['myid'] ; 
        $sql = "UPDATE `pcate` SET `cateName`='{$cateName}',`subid`='{$cateId}',`subCateName`='{$cateName}' WHERE id = {$myid}" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '修改成功!' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败!' ;
        }
    }
    if( $opt == 'delSub' ){
        $id = $_POST['myid'] ;
        $sql = "DELETE FROM pcate WHERE id = {$id}" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $sql1 = "DELETE FROM pcate WHERE subid = {$id}" ;
            $res1 = mysqli_query($link,$sql1);
            $arr['status'] = 1 ;
            $arr['info'] = '删除成功!' ;
        } else {
            $arr['status'] = 0 ; 
            $arr['info'] = '操作失败!' ;
        }
    }
    if( $opt === 'getSubPage' ){
        $perPage = $_POST['perPage'] ;
        $sql = "SELECT COUNT(*) AS c FROM pcate WHERE subid is not null ";
        $res = mysqli_query($link,$sql);
        if( $one = mysqli_fetch_assoc($res) ){
            $pageNum = ceil( $one['c'] / $perPage ) ; 
            $arr['totalPage'] = $pageNum ; 
        }
    }
    
    echo json_encode($arr);
?>